package open.tech.dawn.rest.module.system.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import open.tech.dawn.jdbc.SuperServiceImpl;
import open.tech.dawn.rest.module.system.entity.UserEntity;
import open.tech.dawn.rest.module.system.enums.ManagerStatus;
import open.tech.dawn.rest.module.system.enums.SexEnum;
import open.tech.dawn.rest.module.system.mapper.SysUserMapper;
import open.tech.dawn.rest.module.system.model.SysUser;
import open.tech.dawn.rest.module.system.query.UserQuery;
import open.tech.dawn.rest.module.system.service.ISysRoleService;
import open.tech.dawn.rest.module.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 管理员表 服务实现类
 * </p>
 *
 * @author joss
 * @since 2019-11-07
 */
@Service
public class SysUserServiceImpl extends SuperServiceImpl<SysUserMapper, SysUser> implements ISysUserService {


    @Autowired
    private ISysRoleService sysRoleService;

    @Override
    public int setStatus(String userId, int status) {
        return this.baseMapper.setStatus(userId, status);
    }

    @Override
    public int changePwd(String userId, String pwd) {
        return this.baseMapper.changePwd(userId, pwd);
    }

    @Override
    public Page<UserEntity> selectUsers(Page<UserEntity> page, UserQuery query) {
        List<UserEntity> userEntities = this.baseMapper.selectUsers(page, query);
        if (CollUtil.isNotEmpty(userEntities)) {
            userEntities.forEach(userEntity -> {
                userEntity.setRoleName(sysRoleService.getRoleNameByIds(userEntity.getRoleid()));
                userEntity.setSexName(SexEnum.getSex(userEntity.getSex()).getDesc());
                userEntity.setStatusName(ManagerStatus.valueOf(userEntity.getStatus()));
            });
        }
        page.setRecords(userEntities);
        return page;
    }

    @Override
    public int setRoles(String userId, String roleIds) {
        return this.baseMapper.setRoles(userId, roleIds);
    }

    @Override
    public SysUser getByAccount(String account) {
        if (account == null) {
            return null;
        }
        return this.baseMapper.getByAccount(account);
    }

    @Override
    public String getUserNameById(String userId) {
        SysUser user = baseMapper.selectById(userId);
        if (user != null) {
            return user.getName();
        } else {
            return "--";
        }
    }

}
